void swap (shared_ptr& x) noexcept;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// shared_ptr::swap example
#include <iostream>
#include <memory>
int main () {
std::shared_ptr<int> foo (new int(10));
std::shared_ptr<int> bar (new int(20));
foo.swap(bar);
std::cout << "*foo: " << *foo << '\n';
std::cout << "*bar: " << *bar << '\n';
return 0;
}
*foo: 20 *bar: 10